package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.reggie.common.MyBaseContext;
import com.example.reggie.common.R;
import com.example.reggie.domain.AddressBook;
import com.example.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddresBookController extends BaseController {
    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook) {
        log.info("addressBook: {}", addressBook.toString());
        Long userId = (Long) session.getAttribute("user");
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return R.success("添加地址成功！");
    }

    @GetMapping("/list")
    public R<List<AddressBook>> list() {
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getUserId, userId).orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> addressBookList = addressBookService.list(addressBookLambdaQueryWrapper);
        return R.success(addressBookList);
    }

    @PutMapping("/default")
    public R<String> setDefault(@RequestBody AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> addressBookLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        addressBookLambdaUpdateWrapper.eq(AddressBook::getIsDefault, 1);
        addressBookLambdaUpdateWrapper.set(AddressBook::getIsDefault, 0);
        addressBookService.update(addressBookLambdaUpdateWrapper);

        LambdaUpdateWrapper<AddressBook> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(AddressBook::getId, addressBook.getId());
        lambdaUpdateWrapper.set(AddressBook::getIsDefault, 1);
        addressBookService.update(lambdaUpdateWrapper);
        return R.success("修改默认地址成功！");
    }

    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable long id) {
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getId, id);
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }


    @PutMapping
    public R<String> updateById(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功！");
    }

    @DeleteMapping
    public R<String> delete( Long ids){
        addressBookService.removeById(ids);
        return R.success("删除成功!");
    }

    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        Long userId = MyBaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(userId!=null,AddressBook::getUserId,userId);
        addressBookLambdaQueryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(addressBookLambdaQueryWrapper);
        if(ObjectUtils.isEmpty(addressBook)){
            return R.error("未找到默认地址！");
        }
        return R.success(addressBook);
    }
}
